package com.chaoxuan.uebt.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.adapter.MemberAdapter;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.model.UserIncome;
import com.chaoxuan.uebt.model.UserIncomeList;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.CacheUtils;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

public class MemberActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {
    TextView tvYesterday, tvTotal;
    MemberAdapter adapter;
    int page = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member);

        initTitle();
        tvYesterday = (TextView) findViewById(R.id.tv_yesterday);
        tvTotal = (TextView) findViewById(R.id.tv_total);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        SmartRefreshLayout refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);

        adapter = new MemberAdapter();
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.autoRefresh();
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 0;
        load(true, refreshLayout);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page=adapter.getData().size();
        load(false, refreshLayout);
    }

    private void load(final boolean refresh, final RefreshLayout refreshLayout) {
        User user =CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        subscribe(RetrofitUtils.getAPIService().userIncome(user.getToken(),Const.DEVICE_TYPE), new BaseSubscriber<UserIncome>() {
            @Override
            public void onSuccess(UserIncome data) {
                tvTotal.setText(data.getAll_count());
                tvYesterday.setText(data.getYesterday_count());
            }
        });
        subscribe(RetrofitUtils.getAPIService().userIncomeLists(user.getToken(),page,Const.DEVICE_TYPE), new BaseSubscriber<List<UserIncomeList>>() {
            @Override
            public void onSuccess(List<UserIncomeList> data) {
                Log.d("size:", data.size() + "");
                if (refresh)
                    adapter.getData().clear();

                for(UserIncomeList u:data){
                    u.setNumber(String.valueOf(adapter.getData().size()+1));
                    adapter.addData(u);
                }
                if (refresh) {
                    refreshLayout.finishRefresh();
                } else {
                    refreshLayout.finishLoadMore();
                }
            }
        });
    }

}
